<?php

namespace App\Http\Controllers\Admin;

use App\CodeResponse;
use App\Http\Controllers\Controller;
use App\Models\Permissions;
use App\Models\Role;
use App\Models\RolePermissions;
use App\Models\Users;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;

class RoleController extends AdminController
{
    /**
     * Display a listing of the resource.
     *
     * @param  Request  $request
     * @return void
     */
    public function index(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'page' => 'integer',
            'limit' => 'integer',
            'name' => 'string',
            'sort' => 'string',
            'order' => 'string',

        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $page = $request->input('page',1);
        $limit = $request->input('limit',10);
        $sort = $request->input('sort','created_at');
        $order = $request->input('order','desc');
        $name = $request->get('name', '');
        $data = Role::when($name, function ($query) use ($name) {
            $query->where('name', 'like', "%{$name}%");
        })->orderBy($sort,$order)->paginate($limit, ['*'], 'page', $page);
        return $this->success($data);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ]);

        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make($request->all(), [
            'name' => 'unique:roles,name',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }

        $id = Role::create($request->only('name'))->id;
        $arr = json_decode($request->permissions);
        if(isset($arr) && count($arr) > 0 ){
            foreach ($arr as $v){
                RolePermissions::create([
                    'role_id' => $id,
                    'permissions_id' => $v
                ]);
            }
        }

        return $this->success();
    }


    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \App\Models\Models\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, Role $role)
    {
        $validator = Validator::make($request->all(), [
            'name' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $validator = Validator::make($request->all(), [
            // unique:表名,唯一字段,[排除行的值，以那个字段来排除]
            // 排除id=3的哪行名为name的字段值
            'name' => 'unique:roles,name,' . $request->id . ',id',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::AUTH_NAME_REGISTERED);
        }

        $putData = $request->except(['_method','_token','permissions']);
        Role::where('id',$request->id)->update($putData);

        $rolePermissions = RolePermissions::where('role_id',$request->id);
        $rolePermissions->delete();
        $arr = json_decode($request->permissions);
        if(isset($arr) && count($arr) > 0 ) {
            foreach ($arr as $v) {
                RolePermissions::create([
                    'role_id' => $request->id,
                    'permissions_id' => $v
                ]);
            }
        }

        return $this->success();

    }
    // 角色权限
    public function permissions(Request $request,Role $role) {
        // 读取出所有的权限
        $permissionsAll = (new Permissions())->getAllList();
        // 读取当前角色所拥有的权限
//        $permissions = $role->permissions()->pluck('id','permissions.name')->toArray();
        $permissions = RolePermissions::query()->where('role_id',$request->role_id)->get();
        return $this->success([$permissionsAll,$permissions]);
    }

    // 分配权限
    public function permissionsSave(Request $request,Role $role) {

        $validator = Validator::make($request->all(), [
            'role_id' => 'required|integer|digits_between:1,20|min:1',
            'permissions' => 'required',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $rolePermissions = RolePermissions::where('role_id',$request->role_id);
        $rolePermissions->delete();

        $arr = json_decode($request->permissions);
        foreach ($arr as $v){
            RolePermissions::create([
                'role_id' => $request->role_id,
                'permissions_id' => $v
            ]);
        }
        return $this->success();
    }
    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Models\Role  $role
     * @return \Illuminate\Http\Response
     */
    public function destroy(Request $request)
    {
        $validator = Validator::make($request->all(), [
            'id' => 'required|integer',
        ]);
        if ($validator->fails()) {
            return $this->fail(CodeResponse::PARAM_ILLEGAL);
        }
        $rolePermissions = Role::find($request->id);
        $rolePermissions->delete();
        $rolePermissions = RolePermissions::where('role_id',$request->id);
        $rolePermissions->delete();
        return $this->success();
    }



}
